WEBVTT

00:02.200 --> 00:04.440
Hello everybody and welcome back to the costs.

00:04.440 --> 00:09.200
So in the last session we learned how to check whether that condition is true or false.

00:09.220 --> 00:16.690
And paisan returned as a true or false but so-called boolean objects so typically checking whether their

00:16.690 --> 00:23.260
condition is true or false is only 50 percent of our job because in real world situations we first have

00:23.260 --> 00:29.970
to check whether a condition is true and if so we have to take action one and a false.

00:29.980 --> 00:31.300
We have to take action too.

00:31.300 --> 00:39.820
So here's the point where we introduce if elif and s statements that are doing a very good job here

00:39.820 --> 00:42.070
in Python.

00:42.160 --> 00:46.440
So first of all we define two variables A and B equals five.

00:46.690 --> 00:50.220
And then we check whether A is less than 10.

00:50.220 --> 00:52.590
So we start with the keyword.

00:52.600 --> 01:02.870
If if a less than 10 and then we need a colon and then we press enter and you can see it here.

01:02.890 --> 01:05.340
Python makes automatically and indent.

01:05.560 --> 01:08.770
And now we have to define what our accent is here.

01:08.770 --> 01:10.030
If our statement.

01:10.030 --> 01:10.600
Yes correct.

01:10.600 --> 01:12.700
So obviously our statement is correct.

01:12.700 --> 01:21.830
So our accent should be please print me a is less than 10.

01:22.110 --> 01:28.570
So let's check what happens if we run the cell so we get A's less than 10.

01:28.580 --> 01:29.210
That's correct.

01:29.630 --> 01:36.680
So now what happens if we change here the comparison operator to a is greater than 10.

01:36.950 --> 01:39.330
So obviously the statement is false.

01:39.440 --> 01:43.430
So let's see what happens so Python prints nothing.

01:43.440 --> 01:44.810
So the statement is not true.

01:44.820 --> 01:48.980
So we don't have to take action one which is print A is less than 10.

01:48.990 --> 01:58.740
So now we have to define an alternative action and then we introduce the keyword else you make a colon

01:58.740 --> 02:03.530
on press enter and then you can see also here Python makes automatically and.

02:04.350 --> 02:07.210
And then and then we define our alternative action.

02:07.220 --> 02:10.970
So if the statement is greater than 10 it's not true.

02:11.100 --> 02:14.550
What should be the action and the action here is print

02:17.280 --> 02:22.870
a is not greater than 10.

02:23.190 --> 02:28.690
So let's see what happens if we run the cell we get the message a is not greater than tetanus.

02:28.700 --> 02:29.440
That's correct.

02:29.450 --> 02:31.910
So now if you change again here the operator

02:35.140 --> 02:37.650
is less than 10 and we execute again.

02:37.660 --> 02:40.520
Then we get again is less than 10.

02:40.570 --> 02:46.360
So what Python makes here internally is actually checks whether the conduit condition goes through and

02:46.360 --> 02:51.970
then as it is true it takes the action print and then uh passes and stops.

02:51.990 --> 02:56.100
So there are some sources for mistakes here and statements.

02:56.140 --> 02:57.240
You need to make the colon.

02:57.270 --> 02:57.420
Yeah.

02:57.430 --> 03:01.400
If you forget the colon then we get an error message.

03:01.600 --> 03:03.100
So the colon is important.

03:03.670 --> 03:09.640
And also here the intent you have to make the indent here if you don't make it make the indent here.

03:09.700 --> 03:11.210
You get them error message.

03:13.480 --> 03:19.450
And typically paste and makes the indent automatically if you press here enter after the colon and you

03:19.450 --> 03:22.740
can also by pressing the key tab.

03:22.810 --> 03:29.200
You can make the indent here and also for the eight F statements of the US statement so you have the

03:29.200 --> 03:31.970
same and then there's then the if statement.

03:31.970 --> 03:37.230
So if the h s statement has another indent it also doesn't work.

03:37.240 --> 03:43.840
So the that men should have the same end as the f statement and the axiom here in this case the print

03:43.840 --> 03:46.150
statements should have phone and indent.

03:46.180 --> 03:47.520
So this this year.

03:47.530 --> 03:48.100
Correct.

03:48.100 --> 03:48.510
All right.

03:48.520 --> 03:51.940
So let's move to another example and we compare a and b.

03:51.940 --> 03:58.720
So if a is greater than B which is not the case then please print a is not greater than B.

03:59.530 --> 04:06.160
So if this statement is not true Python then checks if our alternative condition Elif.

04:06.190 --> 04:10.120
So that's the keyword for checking an alternative condition.

04:10.120 --> 04:18.130
So as of a is less than B but to see also not the case please print me is less than B and this is also

04:18.130 --> 04:19.120
false.

04:19.150 --> 04:22.840
So Python continues and then there's the s statement.

04:22.840 --> 04:28.960
So if condition one and condition two they're both false then in all other cases please print me a C

04:28.960 --> 04:35.500
could be a and b in this case of course Python prints a is equal to B and again but Python does internally

04:35.500 --> 04:43.000
it checks in the first condition if it's true it takes action one and stops and if a condition one is

04:43.000 --> 04:49.700
not true it checks a condition two and if condition two is true it takes the action of condition two

04:49.720 --> 04:58.420
and then stops and yeah if all statements are conditions are not true then Python does the action which

04:58.420 --> 05:03.820
is determined by the s statements or for all other cases and we see now on the next example that we

05:03.820 --> 05:06.430
can have more than one elif statement.

05:06.430 --> 05:12.760
So now let's assume that we are ordering some goods in an online web shop and we get some discounts

05:12.760 --> 05:14.460
depending on our price.

05:14.710 --> 05:16.610
And there are some discount targets.

05:16.630 --> 05:23.670
So if the price is lower than 50 dollars we get a zero percent discount if it's between 50 and 100.

05:23.710 --> 05:32.970
So 50 including until 100 excluding we get 10 percent discount if our prices in the entire were 100

05:32.980 --> 05:36.810
including to 200 excluding a 20 percent discount.

05:36.820 --> 05:43.900
And if our price is equal or greater than two hundred dollar or we get a discount of 25 percent we can

05:43.900 --> 05:49.720
transform this into a code and say so if our price is less than our discount and Turnbull at positions

05:49.720 --> 05:57.220
there also if our price is less than 50 you will get a discount of 0 percent and then Elif.

05:57.250 --> 06:04.170
So if this statement is not correct but the price is less than 100 we get a discount of 10 percent.

06:04.210 --> 06:12.090
And if also this condition is not true then we check if our price is less than 200 and also if this

06:12.090 --> 06:17.970
is not true then we know that our price must be greater than 200 or greater equal than two hundred and

06:18.120 --> 06:23.970
in all other cases here greater than two hundred our discount is 25 percent.

06:23.970 --> 06:25.020
So now let's check this.

06:25.020 --> 06:29.660
Our price is seventy five dollars and we run here.

06:29.670 --> 06:35.600
Our if and Elif statements our offer discount price is sixty seven point five.

06:35.910 --> 06:43.350
So we get 10 percent discount because our prices in the interval 50 to 100 where we get 10 percent discount.

06:43.830 --> 06:52.680
And also let's assume if we have a price of 40 so I would assume to get a zero percent discount and

06:52.680 --> 06:54.740
our after this comprises 40.

06:55.390 --> 06:57.750
And also let's assume our prices 1000

07:01.220 --> 07:04.220
we get an after discount prices of 750.

07:04.220 --> 07:06.160
So we get 25 percent discount.

07:06.770 --> 07:09.380
And we can further increase them the elif statement.

07:09.380 --> 07:11.780
So there is no restriction on the Ellis statements.

07:11.780 --> 07:15.230
We cannot fear let's say 50 Elif statements.

07:15.230 --> 07:17.210
So this is quite flexible here.

07:17.210 --> 07:19.760
All right so let's move on to the vile Lou.

07:19.850 --> 07:28.400
So let's set eight to 10 and let's check whether a is less than 20 and if it is less than 20 we increase

07:28.430 --> 07:29.850
a by 1.

07:29.870 --> 07:36.560
So this this year a plus equals one is the same as a equals a plus 1.

07:36.560 --> 07:38.650
So that's an abbreviation here.

07:38.660 --> 07:45.100
We can do that so if a is less than 20 year please increase a by one and print a and let's see what

07:45.100 --> 07:45.480
happens.

07:45.490 --> 07:52.420
A fear on the sale so we get 11 and then we can rerun again because now is eleven and we can check again

07:52.420 --> 07:53.890
if it's less than 20.

07:54.760 --> 07:59.200
Now we got 12 and we can repeat this process many times

08:02.030 --> 08:05.410
until we it reads uh 20 in the end.

08:05.900 --> 08:11.980
So that s second.

08:12.010 --> 08:17.740
So now we have reached 20 and now if you rerun the cell again we check whether a is less than 20 or

08:17.770 --> 08:21.880
so 20 is less than 20 and that is this is a false statement.

08:22.000 --> 08:24.480
And here our loop stops.

08:24.610 --> 08:24.780
Yeah.

08:24.790 --> 08:31.240
And as you can see here I mean it's a bit inconvenient to do this process or to repeat this process

08:31.240 --> 08:32.070
manually.

08:32.230 --> 08:34.850
And they are the while loop comes into play.

08:35.110 --> 08:39.950
So we actually do the same task as Sierra buff but introduce the device statement.

08:39.970 --> 08:45.960
And we define a that's 10 and then we make an hour repeated.

08:46.810 --> 08:55.420
If statement and check whether a is less than 20 and if it's the case we increase a by 1 here and print

08:55.450 --> 08:55.730
a.

08:55.940 --> 08:59.360
And so now let's see what happens.

08:59.440 --> 09:02.030
So Python automatically does um.

09:02.100 --> 09:04.410
The repetition of our if statement.

09:04.420 --> 09:07.850
So you know the first check is um a s 10.

09:07.900 --> 09:14.440
So the first step is uh Python checks whether 10 is less than 20 in this case that increases a by 1

09:14.440 --> 09:15.670
and prints a.

09:15.680 --> 09:20.280
And now Python repeats the process so it takes a equals eleven.

09:20.290 --> 09:22.420
Checks that it's less than 20.

09:22.420 --> 09:23.890
That's correct.

09:23.900 --> 09:25.000
Increases a by 1.

09:25.000 --> 09:26.510
And again prints a lot.

09:26.560 --> 09:31.280
There's twelve Python repeats the process until we reach a equals twenty.

09:31.540 --> 09:32.770
And this is all automated.

09:32.770 --> 09:35.110
So we don't have to to make this manually.

09:35.110 --> 09:37.690
And that's the purpose of a bi loop.

09:37.840 --> 09:42.250
And then in the end we can we can also print again a check.

09:42.250 --> 09:42.540
Okay.

09:42.550 --> 09:44.050
This is our final result.

09:44.440 --> 09:54.020
So the value stopped when a is 20 for let's move onto a more moralistic case here a more real world

09:54.020 --> 10:01.280
situation to say so let's assume we are in charge of loading a container ship and we have to check if

10:01.340 --> 10:08.440
the freight exceeds the maximum freight that is allowed and we assume here that the maximum allowed

10:08.440 --> 10:10.550
weight of 1000 tonnes.

10:10.580 --> 10:18.140
And our actual weight is 1100 tons and we have many containers on our ship and we can reduce our freight

10:18.140 --> 10:20.930
by unloading a container.

10:21.020 --> 10:27.760
And we assume that a container is way to 70 tonnes so past that we can reduce the weight by 17 tonnes.

10:27.830 --> 10:30.500
And so we are introducing here a while loop.

10:30.530 --> 10:37.220
So we are checking if our weight is greater than our maximum allowed weight and if this is the case

10:37.250 --> 10:43.700
we print the caution statement of freight with X tonnes is too heavy and then the next step we reduce

10:43.700 --> 10:50.870
our weight uh with the by our reduction steps so we unload one container and we will put this process

10:51.290 --> 10:57.430
so many times until our actual weight is less or is not greater than our maximum weight.

10:57.650 --> 11:02.450
And in the end we reach it and our while loop stops we print.

11:02.490 --> 11:04.190
Um the final result.

11:04.220 --> 11:06.040
The freight with X tonsils.

11:06.050 --> 11:07.220
Okay.

11:07.380 --> 11:12.230
And um yeah let's assume that our our actual weight is 1100 tonnes.

11:12.230 --> 11:17.930
So it's it's heavier than our max weight and let's check what pace does here.

11:18.110 --> 11:23.330
So it first checks as if our initial weight 1100 tonnes is okay.

11:23.330 --> 11:24.180
No it's not okay.

11:24.200 --> 11:27.650
Then it reduces our weight by 17 tonnes.

11:27.650 --> 11:31.060
And also 1000 eighty three tonnes is too heavy.

11:31.070 --> 11:36.760
And it repeats the steps so many times until we reach afraid of 998 tonnes.

11:36.770 --> 11:44.120
And here the binder stops and we get a printout that nine hundred ninety eight tonnes is okay.

11:44.480 --> 11:46.210
So this works.

11:46.210 --> 11:46.610
All right.

11:46.610 --> 11:54.230
So we learned a lot about if elif and s statements and the Y loop and the next session we are getting

11:54.230 --> 11:59.240
introduced to for loops which are also very important so see you in the next session.

11:59.270 --> 11:59.670
Thank you.

11:59.670 --> 11:59.930
Bye.
